<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h5 class="modal-title">触发条件</h5>
</div>

<div class="main-body" style="height: 500px">

    <div style="display: flex;height: 450px" id="body">
        <div style="width:550px">
            <fieldset>
                <legend>编辑公式区</legend>
                <textarea style="height: 304px; width: 479px; margin: 0px;" id="formulaArea" ></textarea>
            </fieldset>

            <div style="display: flex;margin-top: 10px">
                <label>运算符</label>

                <div class="btn-group" role="group" aria-label="..." style="margin-left: 10px">
                    <button type="button" class="btn btn-default btn-sm">(</button>
                    <button type="button" class="btn btn-default btn-sm">)</button>
                </div>
                <label>|</label>
                <div class="btn-group" role="group" aria-label="...">
                    <button type="button" class="btn btn-default btn-sm">与</button>
                    <button type="button" class="btn btn-default btn-sm">或</button>
                </div>
                <label>|</label>

                <div class="btn-group" role="group" aria-label="...">
                    <button type="button" class="btn btn-default btn-sm">></button>
                    <button type="button" class="btn btn-default btn-sm">>=</button>
                    <button type="button" class="btn btn-default btn-sm">=</button>
                    <button type="button" class="btn btn-default btn-sm"><</button>
                    <button type="button" class="btn btn-default btn-sm"><=</button>
                </div>

                <div class="btn-group" role="group" aria-label="...">
                    <button type="button" class="btn btn-default btn-sm">清除</button>
                    <bustton type="button" class="btn btn-default btn-sm">
                        <a onclick="numberData('/project/arithmeticManage/condition_number')">数字</a>
                    </bustton>
                </div>

                <a href="javascript:void(0)" style="margin-left: 10px" onclick="checkFormula()">公式校验</a>
            </div>
        </div>
        <div style="width:100px">
            <button type="button" class="btn btn-default btn-lg" id="moveButton" data-toggle="tooltip"
                    style="margin-top: 150px;margin-left: 20px;">
                <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
            </button>
        </div>
        <div style="width:550px">
            <div style="margin-right: 10px">
                <div>
                    <!--<label>可选参数/报警/状态/台账属性</label>-->
                    <ul class="nav nav-tabs">
                        <li role="presentation" id="parameter"><a onclick="getParameter()"><label>参数测点</label></a></li>
                        <li role="presentation" id="warn"><a onclick="getWarn()"><label>过程报警</label></a></li>
                        <li role="presentation" id="status"><a onclick="getStatus()"> <label>设备状态</label></a></li>
                        <li role="presentation" id="property"><a onclick="getProperty()"><label>台账属性</label></a></li>
                    </ul>
                </div>

                <table id="dataTable_condition" data-page-list=[]
                       data-striped="true" data-pagination="true" data-page-size="6" data-cache="false">
                </table>
            </div>
        </div>
    </div>

    <div class="modal-footer" style="text-align:center">
        <button type="button" class="btn btn-primary" onclick="makeSure()">确定</button>
    </div>

</div>
<script>

    function numberData(url, id) {
        if (id != null) {
            url += "?id=" + id;
        }
        $("#modal_number").modal({
            remote: url
        });
    }

    //table字段
    var dataFormat = [{
        checkbox: true
    }, {
        field: 'code',
        title: '编号',
        align: 'center'
    }, {
        field: 'name',
        title: '名称',
        align: 'center'
    }, {
        field: 'objName',
        title: '对象',
        align: 'center',
        formatter: function (value, row, index) {
            if (value == 1) {
                return '项目';
            }
            if (value == 2) {
                return '变流器';
            }
            if (value == 3) {
                return '电池包';
            }
            if (value == 4) {
                return '电池模组';
            }
            if (value == 5) {
                return '电池单体';
            }
            if (value == 6) {
                return '平台';
            }
        }
    }];


    function getTableConditon(dataFormat, id, url) {
        $("#" + id).bootstrapTable({
            url: url,
            method: "get",
            sidePagination: "server",
            singleSelect: true,
            dataType: "json",
            queryParams: function queryParams(params) {   //设置查询参数
                var param = {
                    pageNum: params.offset / params.limit,
                    pageSize: 6
                };
                $(".search-data").each(function () {
                    var key = $(this).attr("name");
                    var val = $(this).val();
                    if (val) {
                        param[key] = val;
                    }
                });
                console.log(param);
                return param;
            },
            columns: dataFormat
        });
    }

    $(function () {
        getTableConditon(dataFormat, "dataTable_condition", "/project/arithmeticManage/index_data");
        $('#parameter').addClass("active");
    })

    //移动按钮
    $("#moveButton").click(function () {
        var getSelectRows = $("#dataTable_condition").bootstrapTable('getSelections', function (row) {
            return row;
        });

        if (getSelectRows) {
            for (i = 0; i < getSelectRows.length; i++) {
                var value_text = $("#formulaArea").val().trim();
                $("#formulaArea").val('');
                $("#formulaArea").val(value_text + getSelectRows[i].name.trim());

                var value = $("#touchExpression").val().trim();
                $("#touchExpression").val('');
                $("#touchExpression").val(value + getSelectRows[i].code.trim());
            }
        }
    })


    //公式符号
    $("#body button[name!='openModal']").click(function () {
        var value = $(this).text();
        if (value == '清除') {
            $("#formulaArea").val('');
        }
        else {
            var value_text = $("#formulaArea").val().trim();
            $("#formulaArea").val('');
            $("#formulaArea").val(value_text + value);

            var value_text2 = $("#touchExpression").val().trim();
            $("#touchExpression").val('');
            $("#touchExpression").val(value_text2 + value);
        }
    })
    var count=0;
    //公式校验
    function checkFormula() {
//        debugger;
        var value = $("#formulaArea").val();
        if (value.indexOf("或") > -1) {
            var val = value.substring(value.indexOf("或") - 1, value.indexOf("或"));
            var val1 = value.substring(value.indexOf("或")+1, value.indexOf("或") + 2);
            if(val1=="" || val==""){
                alert("公式不正确");
                count=-1;
                return;
            }else {
                if (val != ")" || val1 != "(") {
                    alert("公式不正确");
                    count=-1;
                    return;
                }else{
                    count=1;
                }
            }
        }
        if (value.indexOf("与") > -1) {
            var val = value.substring(value.indexOf("与") - 1, value.indexOf("与"));
            var val1 = value.substring(value.indexOf("与")+1, value.indexOf("与") + 2);
            if(val1=="" || val==""){
                alert("公式不正确");
                count=-1;
                return;
            }else {
                if (val != ")" || val1 != "(") {
                    alert("公式不正确");
                    count=-1;
                    return;
                }else{
                    count=1;
                }
            }
        }
        if (value.indexOf(">=")>-1) {
            var val = value.substring(value.indexOf(">=") - 1, value.indexOf(">="));
            var val1 = value.substring(value.indexOf(">=") + 2, value.indexOf(">=") + 3);
            if(val1=="" || val==""){
                alert("公式不正确");
                count=-1;
                return;
            }else{
                if (!isNaN(val) || !isNaN(val1)) {
                    count=1;
                } else {
                    alert("公式不正确");
                    count=-1;
                    return;
                }
            }
        }
        if (value.indexOf("<=")>-1) {
            var val = value.substring(value.indexOf("<=") - 1, value.indexOf("<="));
            var val1 = value.substring(value.indexOf("<=") + 2, value.indexOf("<=") + 3);
            if(val1=="" || val==""){
                alert("公式不正确");
                return;
            }else {
                if (!isNaN(val) || !isNaN(val1)) {
                    count=1;
                } else {
                    alert("公式不正确");
                    count=-1;
                    return;
                }
            }
        }

        if (value.indexOf("=")>-1) {
            if (value.indexOf(">=")) {
                var val = value.substring(value.indexOf(">=") - 1, value.indexOf(">="));
                var val1 = value.substring(value.indexOf(">=") + 2, value.indexOf(">=") + 3);
                if (val1 == "" || val == "") {
                    alert("公式不正确");
                    return;
                } else {
                    if (!isNaN(val) || !isNaN(val1)) {
                        count=1;
                    } else {
                        alert("公式不正确");
                        count=-1;
                        return;
                    }
                }
            }
            else if (value.indexOf("<=")>-1) {
                var val = value.substring(value.indexOf("<=") - 1, value.indexOf("<="));
                var val1 = value.substring(value.indexOf("<=") + 2, value.indexOf("<=") + 3);
                if (val1 == "" || val == "") {
                    alert("公式不正确");
                    count=-1;
                    return;
                } else {
                    if (!isNaN(val) || !isNaN(val1)) {
                        count=1;
                    } else {
                        alert("公式不正确");
                        count=-1;
                        return;
                    }
                }
            }
            else {
                var val = value.substring(value.indexOf("=") - 1, value.indexOf("="));
                var val1 = value.substring(value.indexOf("=") + 1, value.indexOf("=") + 2);
                if (val1 == "" || val == "") {
                    alert("公式不正确");
                    count=-1;
                    return;
                } else {
                    if (!isNaN(val) || !isNaN(val1)) {
                    } else {
                        alert("公式不正确");
                        count=-1;
                        return;
                    }
                }
            }
        }
        if (value.indexOf(">")>-1) {
            if (value.indexOf(">=")) {
            } else {
                var val = value.substring(value.indexOf(">") - 1, value.indexOf(">"));
                var val1 = value.substring(value.indexOf(">") + 1, value.indexOf(">") + 2);
                if (val1 == "" || val == "") {
                    alert("公式不正确");
                    count=-1;
                    return;
                } else {
                    if (!isNaN(val) || !isNaN(val1)) {
                        count=1;
                    } else {
                        alert("公式不正确");
                        count=-1;
                        return;
                    }
                }
            }
        }

        if (value.indexOf("<")>-1) {
            if (value.indexOf("<=")) {
            } else {
                var val = value.substring(value.indexOf("<") - 1, value.indexOf("<"));
                var val1 = value.substring(value.indexOf("<") + 1, value.indexOf("<") + 2);
                if (val1 == "" || val == "") {
                    alert("公式不正确");
                    count=-1;
                    return;
                } else {
                    if (!isNaN(val) || !isNaN(val1)) {
                        count=1;
                    } else {
                        alert("公式不正确");
                        count=-1;
                        return;
                    }
                }
            }
        }

        if (value.indexOf("=") != -1 || value.indexOf(">") != -1 || value.indexOf("<") != -1 || value.indexOf(">=") != -1 || value.indexOf("<=") != -1) {
            count=1;
        } else {
            alert("公式不正确");
            count=-1;
            return;
        }

        if(value.indexOf("(")>-1){
            if(value.indexOf(")")>-1){
                count=1;
            }
            else{
                alert("公式不正确");
                count=-1;
                return;
            }
        }
        if(value.indexOf(")")>-1){
            if(value.indexOf("(")>-1){
                count=1;
            }
            else{
                alert("公式不正确");
                count=-1;
                return;
            }
        }

        alert("公式正确");
    }

    function makeSure() {
        if(count==0){
            alert("请进行公式校验");
            return;
        }
        if(count==-1){
            alert("请重新进行公式校验");
            return;
        }
        var value = $("#formulaArea").val();
        $("#touchCondition").val(value);
        $("#touchCondition").text(value);

        var expression = $("#touchExpression").val();
        $("#touchExpression").val(expression);

        $("#modal_condition").modal('hide');  //手动关闭
        $("#model_main_condition").empty();//确保多次打开模态框可以初始化
    }

    function getParameter() {
        $('#parameter').addClass("active");
        $('#parameter').siblings().removeClass("active");
        $("#dataTable_condition").bootstrapTable('destroy');
        getTableConditon(dataFormat, "dataTable_condition", "/project/arithmeticManage/index_data");
    }
    function getWarn() {
        $('#warn').addClass("active");
        $('#warn').siblings().removeClass("active");
        $("#dataTable_condition").bootstrapTable('destroy');
        getTableConditon(dataFormat, "dataTable_condition", "/project/arithmeticManage/warn_data");
    }
    function getStatus() {
        $('#status').addClass("active");
        $('#status').siblings().removeClass("active");
        $("#dataTable_condition").bootstrapTable('destroy');
        getTableConditon(dataFormat, "dataTable_condition", "/project/arithmeticManage/status_data");
    }
    function getProperty() {
        $('#property').addClass("active");
        $('#property').siblings().removeClass("active");
        $("#dataTable_condition").bootstrapTable('destroy');
        getTableConditon(dataFormat, "dataTable_condition", "/project/arithmeticManage/property_data");
    }

</script>